﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Mobile.ContainerModel;

namespace QuickStart
{
	public static class ApplicationRoot
	{
		/// <summary>
		/// Exposes the root application container where global 
		/// services are registered.
		/// </summary>
		/// <devdoc>
		/// The internal setter allows a test project to change 
		/// the container with a test one (need to enable 
		/// InternalsVisibleToAttribute at the assembly level).
		/// </devdoc>
		public static Container Container { get; internal set; }

		/// <summary>
		/// Initializes the root container.
		/// </summary>
		public static void Initialize()
		{
			var container = new Container();

			container
				.Register<IEditCustomersView>(
					c => new EditCustomersView())
				.InitializedBy(
					(c, v) => v.Presenter = c.Resolve<EditCustomersPresenter>());

			container.Register<EditCustomersPresenter>(
				c => new EditCustomersPresenter(
					c.Resolve<IEditCustomersView>(),
					c.Resolve<ICustomerRepository>()));
			
			container.Register<ICustomerRepository>(
					c => new MemoryCustomerRepository())
				.InitializedBy((c, r) =>
				{
					// Initialize with some fake data.
					r.Create(new Customer { FirstName = "Joe", LastName = "Doe" });
					r.Create(new Customer { FirstName = "Sarah", LastName = "Smith" });
					r.Create(new Customer { FirstName = "Mary", LastName = "Brown" });
				});

			Container = container;
		}
	}
}
